package com.lib.framework.widget

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.lib.framework.R
import java.util.*

/**
 * Copyright (C), 2020-2020, 享物说
 * FileName: ToastUtil
 * Author: keanbin
 * Date: 2020-02-05 12:10
 * Description: Toast 工具类
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class ToastUtil {

    companion object {

        /**
         * 显示 Toast
         */
        fun showToast(context: Context, text: String) {
            Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
        }

        /**
         * Toast 显示在正中间
         */
        fun showToastCenter(context: Context, text: String) {
            val toast = Toast.makeText(context, text, Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.show()
        }

        /**
         * 显示 Toast 自定义视图，显示在正中间
         */
        fun showToastView(context: Context, view: View) {
            val mToast = Toast(context)
            mToast.setGravity(Gravity.CENTER, 0, 0)
            mToast.view = view
            mToast.show()
        }

        /**
         * 显示提示Toast：错误样式
         */
        @JvmStatic
        fun showToastViewError(context: Context, text: String) {
            showToastViewDefault(context, text, R.mipmap.ic_error)
        }

        /**
         * 显示提示Toast：正确样式
         */
        @JvmStatic
        fun showToastViewOK(context: Context, text: String) {
            showToastViewDefault(context, text, R.mipmap.ic_ok, 2000)
        }

        /**
         * 显示带图标的默认自定义弹窗，可以定义关闭时间，最长不超过3.5秒，超过无效
         */
        @JvmStatic
        fun showToastViewDefault(context: Context, text: String, resId: Int, toastTime: Long = 3000) {
            val toast = Toast(context)
            val inflate = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val view = inflate.inflate(R.layout.widget_prompt_dialog, null)
            view.findViewById<TextView>(R.id.tv_text).text = text
            view.findViewById<ImageView>(R.id.iv_icon).setImageResource(resId)
            toast.view = view
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.duration = Toast.LENGTH_LONG
            toast.show()
            Timer().schedule(object : TimerTask() {
                override fun run() {
                    toast.cancel()
                }
            }, toastTime)
        }

        /**
         * 显示不带图标的默认自定义弹窗，可以定义关闭时间，最长不超过3.5秒，超过无效
         */
        @JvmStatic
        fun showToastViewDefault(context: Context, text: String, toastTime: Long = 3000) {
            val toast = Toast(context)
            val inflate = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val view = inflate.inflate(R.layout.widget_prompt_dialog02, null)
            view.findViewById<TextView>(R.id.tv_text).text = text
            toast.view = view
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.duration = Toast.LENGTH_LONG
            toast.show()
            Timer().schedule(object : TimerTask() {
                override fun run() {
                    toast.cancel()
                }
            }, toastTime)
        }
    }
}